package com.vancouver.knows.ui.fragment.my;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BSharedPrefManager;

public class UserSetListAdapter extends BaseAdapter {

	private Context mContext;
	private String[] mAttrTitles;
	private LayoutInflater mInflater;
	private BSharedPrefManager sharePrefManager;

	public UserSetListAdapter(Context context, LayoutInflater inflater) {
		this.mInflater = inflater;
		this.mContext = context;
		sharePrefManager = ((MainApplication) context.getApplicationContext()).getSharedPrefManager();
		mAttrTitles = context.getResources().getStringArray(R.array.my_list_titles);
	}

	@Override
	public int getCount() {
		return mAttrTitles.length;
	}

	@Override
	public CharSequence getItem(int position) {
		return mAttrTitles[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewValueHand hand = null;
		View view = convertView;
		if (convertView == null) {
			view = mInflater.inflate(R.layout.my_set_view, parent, false);
			hand = new ViewValueHand(view);

			view.setTag(hand);
		} else {
			hand = (ViewValueHand) view.getTag();
		}

		hand.layout.setBackgroundColor(0);
		hand.title.setText(mAttrTitles[position]);

		if (position == UserFragment.newInstance().getloginOrOutPos()) {
			if (sharePrefManager.getUserId() != null) {
				hand.title.setText(mAttrTitles[position]);
			} else {
				hand.title.setText(mContext.getString(R.string.login));
			}
		}

		return view;
	}

	public class ViewValueHand {
		@Bind(R.id.layout)
		RelativeLayout layout;
		@Bind(R.id.title)
		TextView title;

		public ViewValueHand(View view) {
			ButterKnife.bind(this, view);
		}
	}
}
